package cz.najman.emdroid.helper;

import java.util.Date;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import cz.najman.emdroid.connection.ConnectionMethod;
import cz.najman.emdroid.mappings.Event;
import cz.najman.emdroid.mappings.RegistrationState;
import cz.najman.emdroid.mappings.User;
import cz.najman.emdroid.service.ForwardingResultReceiver;
import cz.najman.emdroid.service.UpdaterService;
import cz.najman.emdroid.utils.WhereClauseBuilder;

public class RegistrationStateHelper extends AbstractHelper {

	private ContentResolver mResolver;
	private WhereClauseBuilder mWhereClauseBuilder;
	
	public RegistrationStateHelper(Context context, ForwardingResultReceiver receiver) {
		super(context, receiver);
		this.mResolver = context.getContentResolver();
		mWhereClauseBuilder = WhereClauseBuilder.getInstance();
		
	}

	private static final String TAG = "EventHelper";	
	
	/* ***************************************************************
	 * 					REMOTE HELPER METHODS 						 *
	 * ***************************************************************/
	
	public void retrieveUsersRegistartionStates(int userId){
		Log.d(TAG, "> retrireveUsersRegistrationStates");
		
		Intent intent = getIntentWithExtra(
				RegistrationState.CONTENT_URI_REGISTRATION_STATES_FOR_USER,
				ConnectionMethod.METHOD_GET,
				new String[] {User.URI_TEMPLATE_STRING},
				new String[] {"" + userId},
				null
			);         
        mContext.startService(intent);
	}
	
	
	public void registerUserOnEvent(int userId, int eventId){
		Log.d(TAG, "> Register on event");
		
		ContentValues values = new ContentValues();
		values.put(RegistrationState.REGISTRATION_DATE, new Date().getTime());
		values.put(RegistrationState.EVENT_ID, eventId);
		values.put(RegistrationState.USER_ID, userId);
		values.put(RegistrationState.STATE, 1);
		
		Intent intent = getIntentWithExtra(
				RegistrationState.CONTENT_URI_REGISTRATION_STATES,
				ConnectionMethod.METHOD_POST,
				new String[] {User.URI_TEMPLATE_STRING, Event.URI_TEMPLATE_STRING},
				new String[] {"" + userId, "" + eventId},
				values
			); 
		
		mContext.startService(intent);
	}
	
	@Override
	protected Intent getIntentWithExtra(Uri uri, int method, String[] parameterTypes, String[] parameterValues, ContentValues values) {
		Intent intent = new Intent(mContext, UpdaterService.class);
		intent.putExtra(UpdaterService.RESULT_RECEIVER,	mResultReceiver);
		intent.putExtra(UpdaterService.OBJECT, RegistrationState.class);
		intent.putExtra(UpdaterService.URI, uri);
		intent.putExtra(UpdaterService.METHOD, method);
		if (parameterTypes != null) {
			intent.putExtra(UpdaterService.PARAMETER_TYPES, parameterTypes);
			intent.putExtra(UpdaterService.PARAMETER_VALUES, parameterValues);
		}
		if (values != null) {
			intent.putExtra(UpdaterService.CONTENT_VALUES, values);
		}

		return intent;
	}
	
	/* ***************************************************************
	 * 					DATABASE HELPER METHODS 					 *
	 * ***************************************************************/
	
	public Cursor getUsersRegistarationStates(int userId){
		
		String whereString = mWhereClauseBuilder.buildWhereClause(RegistrationState.USER_ID, String.valueOf(userId), WhereClauseBuilder.EQUAL);
		
		return mResolver.query(RegistrationState.CONTENT_URI_REGISTRATION_STATES_FOR_USER, null, whereString, null, null);
	}
	
}
